package com.community.community.controller;

import com.community.community.BaseContent;
import com.community.community.pojo.Result;
import com.community.community.pojo.dto.UserDto;
import com.community.community.service.UserService;
import com.community.community.untils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserService userService;

    @PostMapping("/registeer")
    public Result register(@Valid @RequestBody UserDto userDto){
        log.info("注册功能启动");
        String msg = userService.register(userDto);
        if(msg.equals(BaseContent.USERNAME_EXIST)){
            return Result.fail(msg);
        }
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserDto userDto){
        return userService.login(userDto);
    }

    @PostMapping("/update")
    public Result update(@RequestBody UserDto userDto, HttpServletRequest request){
        return userService.update(userDto,request);
    }

    @GetMapping("/info")
    public Result info(HttpServletRequest request) {
        return userService.getInfo(request);
    }

}
